import Vue from 'vue'
import VueRouter from 'vue-router'
import dicts from './modules/dicts'
import hero from './modules/hero'
import news from './modules/news'
import publics from './modules/publics'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/welcome'
  },
  {
    path: '/main',
    component: () => import('@/views/Main/index.vue'),
    children: [
      {
        path: '/main',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        component: () => import('@/views/Welcome')
      },
      ...dicts,
      ...hero,
      ...news,
      ...publics
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
  },
  {
    path: '/register',
    component: () => import('@/views/Register')
  }

]

const router = new VueRouter({
  routes
})

/* router.beforeEach((to, from, next) => {
  // 1. 验证是否是非登录
  if (to.path !== '/login') {
    // 2. 验证token是否存在
    if (localStorage.token) {
      // 存在 next
      next()
    } else {
      // token不存在跳转到 登录页面
      next('/login')
    }
  } else {
    next()
  }

  // if (!localStorage.token && !to.meta.isPublic) {
  //   next('api/user/login')
  // } else {
  //   next()
  // }
}) */
router.beforeEach((to, from, next) => {
  if (!localStorage.token && to.path !== '/login' && to.path !== '/register') {
    next('/login')
  } else {
    next()
  }
})

export default router
